<template lang="pug">
    skin(@up="up",@down="down",@back="back")
        div(class="flex-column charge-exhaust-exhaust")
            div(class="flex-column-item center flex-center-center h3 under-line")
                span ----推进排气----
            div(class="flex-column-item flex-3", v-if="status=='ready'")
                div(class="flex-column")
                    div(class="flex-column-item flex-center-center")
                        span 按↑开始排气
                    div(class="flex-column-item flex-center-center")
                        div 注意！排气前
                    div(class="flex-column-item flex-center-center selected-text") 请断开输注管路！
            div(class="flex-column-item flex-3", v-if="status=='pause'")
                div(class="flex-column")
                    div(class="flex-column-item flex-center-center")
                        span 按↑开始排气
                    div(class="flex-column-item flex-center-center")
                        div 按[ESC]退出！
                    div(class="flex-column-item flex-center-center")
                        span 暂停中: {{remaining}}
            div(class="flex-column-item flex-3", v-if="status=='working'")
                div(class="flex-column")
                    div(class="flex-column-item flex-center-center")
                        span 按↓键暂停排气
                    div(class="flex-column-item flex-center-center")
                        div 按[ESC]退出！
                    div(class="flex-column-item flex-center-center")
                        span 排气中: {{remaining}}
</template>

<script>
    export default {
        data() {
            return {
                page: {maxResultCount: 3, skipCount: 0},
                exhausting: false, // 复位完成
                interval: null,
                timeout: null,
                exhaustPause: null,
                interval: null,
                remaining: 0, // 剩余的排气量
                exhaustAmount: 0, // 排气总量
                status: "ready", // ready:准备中 pause:暂停 working:排气中
            };
        },
        watch: {
            status(val) {
                if (this.interval) clearInterval(this.interval);
                if (val != 'working') return;

                this.interval = setInterval(function () {
                    let afterReduce = (window.excharge.exhaustTotal * 10 - 1.2 * 10) / 10; // 减去1.2后的剩余可以排气的量
                    if (afterReduce > 0) {
                        window.excharge.exhaustTotal = afterReduce;
                        this.exhaustAmount = (this.exhaustAmount * 10 + 1.2 * 10) / 10; // 已经排气的量加到以排气的总量总
                        window.dose.remainingAmount = (window.dose.remainingAmount * 10 + 1.2 * 10) / 10; // 需要加到全局的总共剩下多少胰岛素上
                    } else {
                        window.dose.remainingAmount = (window.dose.remainingAmount * 10 + window.excharge.exhaustTotal * 10) / 10; // 需要加到全局的总共剩下多少胰岛素上

                        this.exhaustAmount = (this.exhaustAmount * 10 + window.excharge.exhaustTotal * 10) / 10; // 剩余的量加入到总量中
                        window.excharge.exhaustTotal = 0;
                        this.exhausting = false;
                        clearInterval(this.interval);
                        if (this.exhaustAmount > 0) {
                            window.excharge.exhaustRecords.push({date: new Date, amount: this.exhaustAmount});
                            this.exhaustAmount = 0;
                        }
                    }


                    this.remaining = window.excharge.exhaustTotal;
                }.bind(this), 500);
            }
        },
        methods: {
            up() {
                this.status = 'working';
            },
            down() {
               if(this.status == 'working') this.status = 'pause';
            },

            back() {
                clearInterval(this.interval);
                if (this.exhaustAmount > 0) {
                    // 退出要写入记录
                    window.excharge.exhaustRecords.push({date: new Date, amount: this.exhaustAmount});
                    this.exhaustAmount = 0;
                }
                this.$router.push("index?index=0");
            }
        }
    };
</script>

<style lang="less">

</style>
